package com.unieagle.exam.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.preference.PreferenceManager;

public final class SharedPreferenceUtils
{
    private static final String USERINFO = "user_info";
    
    private static final String FREE_EXAM_CNT = "free_exam_cnt"; //免费考试五次
    
    private static final String LAST_UPDATED_TIME = "last_update";
    
    private static final String PUSH_TOKEN = "push_token";
    
    public static SharedPreferences getPreferences(final Context context)
    {
        return PreferenceManager.getDefaultSharedPreferences(context);
    }

    public static Editor getEditor(final Context context)
    {
        return PreferenceManager.getDefaultSharedPreferences(context).edit();
    }
    
    public static int getErrorCount(final Context ctx, long id) {
    	SharedPreferences prefs = getPreferences(ctx);
        return prefs.getInt(String.valueOf(id), 0);
    }
    
    public static void  setErrorCount(final Context ctx, long id, int count) {
    	SharedPreferences.Editor editor = getEditor(ctx);
        editor.putInt(String.valueOf(id), count);
        editor.commit();
    }

    public static String getUserInfo(Context ctx){
        SharedPreferences prefs = getPreferences(ctx);
        return prefs.getString(USERINFO, "");
    }

    public static void setUserInfo(Context ctx,String text){
        SharedPreferences.Editor editor = getEditor(ctx);
        editor.putString(USERINFO, text);
        editor.commit();
    }
    
    public static int getFreeExamCount(final Context ctx) {
    	SharedPreferences prefs = getPreferences(ctx);
        return prefs.getInt(FREE_EXAM_CNT, 5);
    }
    
    public static void setFreeExamCount(final Context ctx, int count) {
    	SharedPreferences.Editor editor = getEditor(ctx);
        editor.putInt(FREE_EXAM_CNT, count);
        editor.commit();
    }
    
    public static String getLastUpdatedTime(final Context ctx) {
    	SharedPreferences prefs = getPreferences(ctx);
        return prefs.getString(LAST_UPDATED_TIME, "1970-01-01 00:00:00 UTC");
    }
    
    public static void setLastUpdatedTime(final Context ctx, String time) {
    	SharedPreferences.Editor editor = getEditor(ctx);
        editor.putString(LAST_UPDATED_TIME, time);
        editor.commit();
    }
    
    public static String getPushToken(final Context ctx) {
    	SharedPreferences prefs = getPreferences(ctx);
        return prefs.getString(PUSH_TOKEN, "");
    }
    
    public static void setPushToken(final Context ctx, String token) {
    	SharedPreferences.Editor editor = getEditor(ctx);
        editor.putString(PUSH_TOKEN, token);
        editor.commit();
    }

}